package com.wyg.blog.controller.view_blog;

import com.wyg.blog.model.blog.Tag;
import com.wyg.blog.model.blog.Type;
import com.wyg.blog.service.blog.BlogService;
import com.wyg.blog.service.blog.BlogTagService;
import com.wyg.blog.service.blog.TagService;
import com.wyg.blog.service.blog.TypeService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/7 21:50
 */
@RestController
@RequestMapping("/api/view/type")
public class ViewTypeController {
    @Autowired
    TypeService typeService;

    @Autowired
    TagService tagService;

    @Autowired
    BlogTagService blogTagService;

    @Autowired
    BlogService blogService;

    @GetMapping("/getAllTypes")
    @CrossOrigin
    public JsonResult<List<Type>> getAllTypes(){
        List<Type> types = typeService.selectAllTypes();
        return new JsonResult<>(types, "200", "全部分类查询成功!");
    }

    @GetMapping("/getAllTags")
    @CrossOrigin
    public JsonResult<List<Tag>> getAllTags(){
        List<Tag> tags = tagService.selectAllTags();
        return new JsonResult<>(tags, "200", "全部标签查询成功!");
    }

    @GetMapping("/getPartTypes")
    @CrossOrigin
    public JsonResult<List<Map<String, String>>> getPartTypes(){
        List<Type> types = typeService.selectAllTypes();
        List<Map<String, String>> results = new ArrayList<>();
        for(int i = 0; i < types.size(); i++){
            Type type = types.get(i);
            Map<String, String> result = new HashMap<>();
            result.put("name", type.getType());
            result.put("value", type.getId().toString());
            results.add(result);
        }
        return new JsonResult<>(results, "200", "全部分类查询成功!");
    }

    @GetMapping("/getPartTags")
    @CrossOrigin
    public JsonResult<List<Map<String, String>>> getPartTags(){
        List<Tag> tags = tagService.selectAllTags();
        List<Map<String, String>> results = new ArrayList<>();
        for(int i = 0; i < tags.size(); i++){
            Tag tag = tags.get(i);
            Map<String, String> result = new HashMap<>();
            result.put("name", tag.getTag());
            result.put("value", tag.getId().toString());
            results.add(result);
        }
        return new JsonResult<>(results, "200", "全部标签查询成功!");
    }

    @GetMapping("/getLatestTags")
    @CrossOrigin
    public JsonResult<List<Map<String,String>>> getLatestTags(){
        List<Tag> tags = tagService.selectLatestTags();
        List<Map<String,String>> results = new ArrayList<>();
        for(int i = 0; i < tags.size(); i++){
            Tag tag = tags.get(i);
            Map<String,String> result = new HashMap<>();
            result.put("id", tag.getId().toString());
            result.put("number", blogTagService.selectBlogCount(tag.getId()).toString());
            result.put("tag_name", tag.getTag());
            results.add(result);
        }
        return new JsonResult<>(results,"200","最新分类查询成功!");
    }

    @GetMapping("/getLatestTypes")
    @CrossOrigin
    public JsonResult<List<Map<String,String>>> getLatestTypes(){
        List<Type> types = typeService.selectLatestTypes();
        List<Map<String,String>> results = new ArrayList<>();
        for(int i = 0; i < types.size(); i++){
            Type type = types.get(i);
            Map<String,String> result = new HashMap<>();
            result.put("id", type.getId().toString());
            result.put("number", blogService.selectBlogCount(type.getId()).toString());
            result.put("type_name", type.getType());
            results.add(result);
        }
        return new JsonResult<>(results,"200","最新标签查询成功!");
    }

    @GetMapping("/getCounts")
    @CrossOrigin
    public JsonResult<Map<String, Integer>> getCounts() {
        Integer blogCount = blogService.selectAllBlogsCount();
        Integer tagCount = tagService.selectTagCount();
        Integer typeCount = typeService.selectTypeCount();
        Map<String, Integer> result = new HashMap<>();
        result.put("blogs", blogCount);
        result.put("tags", tagCount);
        result.put("types", typeCount);
        return new JsonResult<>(result, "200", "获取成功!");
    }
}
